import { Router } from 'express'
import { sendMessage } from '../controllers/chat.js'
import logger from '../services/logger.js'

const router = Router()

// 添加调试中间件
router.use((req, res, next) => {
  logger.info(`Chat route: ${req.method} ${req.originalUrl}`)
  next()
})

// 测试路由
router.get('/test', (req, res) => {
  res.json({ message: 'Chat route is working' })
})

router.get('/', async (req, res, next) => {
  try {
    logger.info('Handling chat request:', {
      method: req.method,
      query: req.query,
      url: req.originalUrl
    })
    await sendMessage(req, res, next)
  } catch (error) {
    logger.error('Chat error:', error)
    next(error)
  }
})

router.post('/', sendMessage)

export default router 